package cn.sc.summer.warm.config;

import cn.sc.summer.redis.util.RedisHelper;
import cn.sc.summer.warm.endpoint.WarmupEndpoint;
import cn.sc.summer.warm.service.WarmUpService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 类名：热启动配置类
 *
 * @author a-xin
 * @date 2024/8/26 11:00
 */
@Configuration
public class WarmUpConfig {

    @Bean
    @ConditionalOnBean(WarmUpService.class)
    @ConditionalOnProperty(prefix = "project.warmup", name = "enabled", havingValue = "true")
    public Boolean warmUpRegister(WarmUpService warmUpService) {
        try {
            RedisHelper.del(WarmupEndpoint.WARM_UP_KEY);
            warmUpService.register();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        return Boolean.TRUE;
    }

}
